package org.budo.support.spring.io.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.budo.support.lang.util.FileUtil;
import org.budo.support.lang.util.IoUtil;
import org.budo.support.lang.util.ReflectUtil;
import org.budo.support.lang.util.StringUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;

import com.alibaba.dubbo.common.utils.StringUtils;

/**
 * @author lmw
 */
public class ResourceUtil {
    public static Class<?>[] _readLineToTypes(Resource[] resources) {
        if (null == resources || resources.length < 1) {
            return new Class<?>[0];
        }

        Set<String> interfaceTypeNames = new HashSet<String>();
        for (Resource resource : resources) {

            InputStream inputStream = ResourceUtil.getInputStream(resource);
            Set<String> lines = IoUtil.bufferedReadLineToStringSet(inputStream);
            interfaceTypeNames.addAll(lines);
        }

        List<Class<?>> interfaceTypes = new ArrayList<Class<?>>();
        for (String interfaceTypeName : interfaceTypeNames) {
            if (StringUtils.isEmpty(interfaceTypeName)) {
                continue; // 空行跳过
            }

            if (interfaceTypeName.trim().startsWith("#")) {
                continue; // #开头为注释，跳过
            }

            Class interfaceType = ReflectUtil.classForName(interfaceTypeName);
            interfaceTypes.add(interfaceType);
        }

        return interfaceTypes.toArray(new Class<?>[interfaceTypes.size()]);
    }

    public static String toString(Resource[] resources) {
        int length = resources.length;
        String[] strings = new String[length];
        for (int i = 0; i < length; i++) {
            Resource resource = resources[i];
            strings[i] = resource + "(" + resource.getFilename() + ")";
        }
        return "[" + StringUtil.join(strings, ", ") + "]";
    }

    public static UrlResource urlResource(String path) {
        try {
            return new UrlResource(path);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    public static ClassPathResource classPathResource(String path) {
        return new ClassPathResource(path);
    }

    public static InputStream getInputStream(Resource resource) {
        try {
            return resource.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static URI getURI(Resource resource) {
        try {
            return resource.getURI();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static File getFile(Resource resource) {
        if (null == resource) {
            return null;
        }

        try {
            return resource.getFile();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String classPathResourceInputStreamToString(String path) {
        InputStream inputStream = classPathResourceInputStream(path);
        return IoUtil.toString(inputStream);
    }

    public static InputStream classPathResourceInputStream(String path) {
        ClassPathResource resource = classPathResource(path);
        return getInputStream(resource);
    }

    public static String classPathResourceFileCanonicalPath(String path) {
        ClassPathResource classPathResource = classPathResource(path);
        File file = getFile(classPathResource);
        return FileUtil.getCanonicalPath(file);
    }
}